package pers.cz.job;

import pers.cz.config.ExtentTestNGIReporterListener;
import pers.cz.quartz.job.AbstractQuartzJob;
import pers.cz.queue.RequestQueue;
import pers.cz.testNg.TestNg;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;
import org.testng.TestNG;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @program: PostGirl-panent
 * @description: TestCaseJob
 * @author: Cheng Zhi
 * @create: 2021-07-23 15:54
 **/
@Component
public class TestCaseJob extends AbstractQuartzJob {

    @Override
    public void doJob(JobExecutionContext var1, Map params) throws JobExecutionException {
        Long jobId = (Long) var1.getJobDetail().getJobDataMap().get("jobId");
        String jobName = (String) var1.getJobDetail().getJobDataMap().get("jobName");
        String param = (String) var1.getJobDetail().getJobDataMap().get("param");
        String url = (String) var1.getJobDetail().getJobDataMap().get("url");
        Integer userId = (Integer) var1.getJobDetail().getJobDataMap().get("userId");
        String[] splits = param.split(",");
        List<Integer> ids = new ArrayList<Integer>();
        for (String split : splits) {
            ids.add(Integer.valueOf(split));
        }

        String value = param + "^" + url + "^" + userId;
        RequestQueue.addQueue(value);
        List<String> testNameList = new ArrayList<>();
        testNameList.add(ids.toString());
        TestNG testNG = new TestNG();
        Class[] classes = {TestNg.class};
        testNG.setTestClasses(classes);
        testNG.setDefaultSuiteName(ids.toString());
        testNG.setTestNames(testNameList);
        testNG.addListener(new ExtentTestNGIReporterListener((Long) params.get("JOB_LOG_SONBR")));
        testNG.run();

    }

}
